$(document).ready(function () {

    $('#caledar_from').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'dd-mm-yy',
        yearRange: '1930:2050'
    });
    
    $('#caledar_to').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'dd-mm-yy',
        yearRange: '1930:2050'
    });
    
    //Custom Form
    $('.custom_chk').jqTransCheckBox();
    $('.custom_rd').jqTransRadio();
    $('.custom_select').jqTransSelect();

    //FancyBox
    $('.fancyboxClick').fancybox();

    //Show Loading
    $('#saveContent').click(function () {
        $('#loader').fadeIn();
    });

    //Parse URL
    /*var url = $.url(document.location.href);
    if (url.segment(-1) != 'update' && url.segment(-2) != 'update') {
        if (url.fsegment(1) == 'back' || url.fsegment(1) == 'save') {
            if ($('#start').val() == '') {
                $('#start').val(0);
            }
            searchContent($('#start').val(), 10);
        } else {
            if (url.segment(-1) != 'update_profile') {
                //Load Content
                if (module != 'admin') {
                    searchContent(0, 10);
                }
            }
        }
    }*/
});


function searchContent(start,per_page){
	if(per_page==undefined){
		if ($('#per_page').val()) {
			per_page = $('#per_page').val();
		} else {
			per_page = 10;
		}
	}
	var funcSort = $('#func_sort').val();
	var typeSort = $('#type_sort').val();
	$.post('/admin/manageuser/alluser',{
		func_order_by: funcSort,
		order_by: typeSort,
		start: start,
		per_page: per_page,
		dateFrom: $('#caledar_from').val(),
		dateTo: $('#caledar_to').val(),
		content: $('#search_content').val()
	},function(data){
		$('#ajax_loadContent').html(data);

		//Custom Form
		$('.custom_chk').jqTransCheckBox();
		
		//FancyBox
		$('.fancyboxClick').fancybox();
		
		//Set Icon Order By
		$('.sort').removeClass('icon_sort_desc');
		$('.sort').removeClass('icon_sort_asc');
		$('.sort').addClass('icon_no_sort');
		if(typeSort=='DESC'){
			$('#'+funcSort).addClass('icon_sort_desc');
		}else{
			$('#'+funcSort).addClass('icon_sort_asc');
		}
	});
}

function enterSearch(e){
	if (e.keyCode == 13){ 
		searchContent(0);
	}
}

function sort(func){
	var func_sort = $('#func_sort').val();
	var type_sort = $('#type_sort').val();
	if(func==func_sort){
		if(type_sort=='DESC'){
			$('#type_sort').val('ASC');
		}else{
			$('#type_sort').val('DESC');
		}
	}else{
	    $('#func_sort').val(func);
		$('#type_sort').val('DESC');
	}
	searchContent(0,$('#per_page').val());
}

function updateStatus(id,status,module){
	var url ='/admin/'+module+'/UpdateStatus';
	$.post(url,{
			id: id,
			status: status
		},
		function (data) {
		    if (data == "0") {
		        alert("You can not update! Please check info");
		        return false;
		    } else {
		        $('#loadStatusID_' + id).html(data);
		    }
		}
	);
}

function updateStatusLeague(id, status, module) {
    var url = '/admin/' + module + '/UpdateStatus';
    $.post(url, {
        id: id,
        status: status
    },
		function (data) {
		    if (data == "0") {
		        alert("You can not update League! Because has match upcomming");
		        return false;
		    } else {
		        $('#loadStatusID_' + id).html(data);
		    }
		}
	);
}

function selectItem(id){
	var itemCheck = document.getElementById('item'+id);
	if(itemCheck.checked==false){
		$('.item_row'+id).addClass('row_active');
	}else{
		$('.item_row'+id).removeClass('row_active');
	}
}

function selectAllItems(max){
	if(document.getElementById('selectAllItems').checked==false){
		$('.jqTransformCheckboxWrapper a').addClass('jqTransformChecked');
		for(var i=0;i<max;i++){
			if(document.getElementById('item'+i)!=null){
				$('.item_row'+i).addClass('row_active');
				itemCheck = document.getElementById('item'+i);
				itemCheck.checked = true;
			}
		}
	}else{
		$('.jqTransformCheckboxWrapper a').removeClass('jqTransformChecked');
		for(var i=0;i<max;i++){
			if(document.getElementById('item'+i)!=null){
				$('.item_row'+i).removeClass('row_active');
				itemCheck = document.getElementById('item'+i);
				itemCheck.checked = false;
			}
		}
	}
}

function showStatusAll(){
	var max = $('#per_page').val();
	for(var i=0;i<max;i++){
		if(document.getElementById('item'+i)!=null){
			if(document.getElementById('item'+i).checked==true){
				updateStatus($('#item'+i).val(),0,module);
			}
		}
	}
}

function hideStatusAll(){
	var max = $('#per_page').val();
	for(var i=0;i<max;i++){
		if(document.getElementById('item'+i)!=null){
			if(document.getElementById('item'+i).checked==true){
				updateStatus($('#item'+i).val(),1,module);
			}
		}
	}
}

function deleteItem(id){
	var del = confirm("Are you sure delete item?");
	if(del){
		var url = root+'admincp/'+module+'/delete';
		$.post(url,{
				id: id
			},function(data){
				searchContent(0,$('#per_page').val());
			}
		);
	}
}

function deleteAll(){
	var del = confirm("Are you sure delete item selected?");
	if(del){
	    var max = result;
		for(var i=0;i<max;i++){
			if(document.getElementById('item'+i)!=null){
				if(document.getElementById('item'+i).checked==true){
				    id = $('#item' + i).val();
				    var url = root + 'Admin/' + controller + '/Delete';
				    $.post(url, { id: id }, function (data) {
				        if (data == '1') {
				            LoadAjax(root, controller, page);
				            return false;
				        }
				        else {
				            alert("Delete not success");
				            return false;
				        }
				    });
				}
			}
		}
	}
}


function InPlay() {
    var cfmsg = confirm("Are you sure InPlay Match selected?");
    if (cfmsg) {
        var max = result;
        for (var i = 0; i < max; i++) {
            if (document.getElementById('item' + i) != null) {
                if (document.getElementById('item' + i).checked == true) {
                    id = $('#item' + i).val();
                    var url = root + 'Admin/' + controller + '/InPlay';
                    $.post(url, { id: id }, function (data) {
                        if (data == "2") {
                            alert("Match is InPlay! You can not start again");                                                        
                            return false;
                        }
                        if (data == 1) {
                            alert("Match is closed! You can not start again");                            
                            return false;
                        }
                        if (data == "lock") {
                            alert("Match is locked! You must unlock to start agian");
                        }
                        if (data == "sucess") {
                            alert("InPlay match sucessfull");
                            LoadAjax(root, controller, page);
                            return false;
                        }
                    });
                }
            }
        }
    }
}

function EndPlay() {
    var urlEndPlay = '/Admin/Match/EndPlayAble';    
    var cfmsg = confirm("Are you sure close match selected?");
    if (cfmsg) {
        var max = result;
        for (var i = 0; i < max; i++) {
            if (document.getElementById('item' + i) != null) {
                if (document.getElementById('item' + i).checked == true) {
                    id = $('#item' + i).val();
                    $.post(urlEndPlay, { id: id }, function (data) {
                        if (data == 'inplay') {
                            var url = root + 'Admin/' + controller + '/EndPlay/' + id;
                            location.href = url;
                        }
                        if(data=='closed') {
                            alert("Match is closed! You can not EndPlay again");
                            return false;
                        }                        
                    });
                    
                }
            }
        }
    }
}

function LoadAjax(root,controller,index) {
    var url = root + 'admin/' + controller + '/loadajax';
	$.post(url, { page: index}, function (data) {
	    $("#ajax_loadContent").html(data);	    
	    convertAjaxContent();
	});
}

function convertAjaxContent() {
    $("#ajax_loadContent div.pagination. a").click(function(event) {
        event.preventDefault();        
        var target = $(this).attr("href");
        $.post(target, function(data) {
            $("#ajax_loadContent").html(data);
            convertAjaxContent();
        });
    });
}

function createObj() {
    var createObjLink = "";
    if (controller == 'Match'
        || controller == 'SoccerTeam'
        || controller == 'League'
        || controller == 'Common'
        || controller == 'SecureStore'
        || controller == 'Announcement') {
        createObjLink = '/Admin/' + controller + '/Modify';
    }
    if (createObjLink == "") {
        alert("You can't add object");
        return false;
    }
    var url = createObjLink;
    location.href = url;
}





